<?php
defined('BASEPATH') or exit('No direct script access allowed');

use Core\AdminController;
use Models\Money_log;
use Models\User;
use Models\UserLevel;

class UserController extends AdminController
{


    /**
     * 用户列表
     */
    public function index()
    {
        $keyword = $this->input->get('keyword');
        $user = new User;
        // $user->login('','');
        if ($keyword) {
            $user->like('username', $keyword);
            $user->or_like('phone', $keyword);
            $user->or_like('nickname', $keyword);
        }
        $users = $user->paginate(20);
        $user_levels = UserLevel::where('1=1')->get();
        $this->load->view('admin/user/list', array(
            'users'=>$users,
            'user_levels' => $user_levels
        ));
    }

    public function detail($id)
    {
        $user = (new User)->find($id);
        unset($user['password']);

        echo json_encode($user);
    }

    /**
     * 用户信息处理
     */
    public function deal()
    {
        $user = new User;

        $this->load->library('form_validation');
        $this->form_validation->set_rules('nickname', '昵称', 'trim')
        ->set_rules('first_name', '姓', 'trim')
        ->set_rules('last_name', '名称', 'trim')
        ->set_rules('wechat', '微信号', 'trim')
        ->set_rules('phone', '手机号', 'numeric')
        ->set_rules('email', '邮箱', 'trim')
        ->set_rules('birthday', '名称', 'trim')
        ->set_rules('sex', '性别', 'integer')
        ->set_rules('status', '锁定', 'integer')
		->set_rules('level', '等级', 'integer');
        if (!$this->input->post('id')) {
            $this->form_validation->set_rules('phone', '手机号', 'is_unique[users.phone]');
        }
        if ($this->form_validation->run()) {
            $post = $this->input->post();
            foreach ($post as $key => $value) {
                if ($value === '') {
                    unset($post[$key]);
                }
            }
            $user->load($post);
            if ($user['first_name'] || $user['last_name']) {
                $user['username'] = get_username($user['first_name'], $user['last_name']);
            }

            if ($user['password']) {
                $user['password'] = password_hash($user['password'], PASSWORD_DEFAULT);
            }
            if (!$this->input->post('id')) {
                $user['create_at'] = date('Y-m-d H:i:s');
                $user->add();
            } else {
                $user->update();
            }

            return $this->response([
                'code' => 0,
                'msg' => 'ok'
            ]);
        } else {
            return $this->response([
                'code' => 1,
                'msg' => $this->form_validation->error_string()
            ]);
        }
    }

    public function recharge($id)
    {
        $fee = $this->input->post('fee');

        $user = (new User)->find($id);
        if ($user) {
            $user['balance'] += $fee;
            // 加入记录
            $money_log = new Money_log;
            $this->db->trans_start();
            $user->update();
            $money_log->load([
                'type' => 5,
                'user_id' => $id,
                'num' => $fee,
                'desc' => '后台充值',
                'pay_type' => 7,
                'create_time' => date('Y-m-d H:i:s')
            ]);
            $money_log->add();
            $this->db->trans_complete();

            return $this->response(['code' => 0]);
        }
    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = intval($this->input->post('id'));
        $user = new User;
        $user->delete($id);
        echo json_encode(array('code'=>0,'msg'=>'ok'));
    }

    public function multi_add()
    {
        if ($this->input->method() == 'get') {
            return $this->load->view('admin/user/multi_add');
        }

        $users = $this->input->post('users');
        if (!$users) {
            return $this->response(['error' => '没有数据'], 422);
        }

        foreach ($users as $key => $value) {
            $users[$key]['username'] = get_username($value['first_name'], $value['last_name']);
        }

        $result = $this->db->insert_batch('users', $users);
        if ($result) {
            return $this->response();
        } else {
            return $this->response([], 500);
        }
    }
}
